// JavaScript Document
(function(jQuery) {
    jQuery.fn.clickoutside = function(callback) {
        var outside = 1, self = $(this);
        self.cb = callback;
        this.click(function() {
            outside = 0;
        });
        $(document).click(function(event){
            if(event.button == 0)
            {
                outside && self.cb(event);
                outside = 1;
            }
        });
        return $(this);
    }
})(jQuery);

$(function(){
    showContent('box_hot_news');
    showContent('box_cauthuxs');
    showContent('box_daily_bongda');
    showContent('box_bxh_bongda');
    showContent('box_video_hot');
    $('.chose_item').click(function(){
        $(this).next().slideToggle(200, function(){
            if($(this).is(':hidden'))
            {
                $(this).prev().removeClass('active');
            }
            else
            {
                $(this).prev().addClass('active');
            }
	   
        });
    });
    //    $('.chose_item').clickoutside(function(){
    //        if($(this).next().css('display') == 'block'){
    //            $(this).removeClass('active');
    //            $(this).next().hide();
    //        }
    //    });
    //    $('#gallery').clickoutside(function(){
    //        if($('.main_ct_popup').css('display') == 'block'){
    //            $('.main_ct_popup').fadeOut(500);
    //            $('body').css('overflow' , 'auto');
    //        }
    //    });
	
    var tmnu;
    $('.chose_league_item a.icon_down').hover(
        function(){
            $(this).addClass('over')
            objmnu=$(this).next('.list_league');
            if($(objmnu).css('display')!='none'){
                clearTimeout(tmnu);
            }
            else{
                $('.chose_league_item .list_league').hide();
	   
                clearTimeout(tmnu);
            }
            $(objmnu).fadeIn(100);
            $(this).removeClass('over')
        },
        function(){
            $(this).removeClass('over')
            tmnu=setTimeout(function(){
                $(objmnu).hide();
	   
            },100);
	 	
        }
        );
    $('.chose_league_item .list_league').hover(
        function(){
            clearTimeout(tmnu);
            $(this).show();
            $('.chose_league_item a.icon_down').addClass('over')
        },
        function(){
            tmnu=setTimeout(function(){
                $(objmnu).hide();
            },100);
            $('.chose_league_item a.icon_down').removeClass('over')
        });
    $('.confirm_comment .btn_send_comment').hover(
        function(){
            $(this).addClass('over');
        },
        function(){
            $(this).removeClass('over');
        }
        );
})

function showContent(idname){
    $('#'+idname+' .title_tab li a').click(
        function(){
            var itemTab =  $(this).attr('rel');
            $('#'+idname+' .title_tab li').removeClass('active');
            $(this).parent().addClass('active');
            $('#'+idname+' .content_tab').hide();
            $('#'+idname+' .'+itemTab+'').fadeIn(400);
        }
        );
}

///* Get content table ranking by league and season */
//$(function(){
//    $('a.bxh_tab_link').click(function(event){
//        event.preventDefault();
//        var $this = $(this);
//        var href = $this.attr('rel');
//        var keyCache = href;
//        var cachedContent = null;
//        /* Get content by jcache */
//        try 
//        {
//            cachedContent = $.jCache.getItem(keyCache);
//        } catch (ex) {
//            cachedContent = null;
//        }
//        
//        if (cachedContent == null || typeof cachedContent == 'undefined')
//        {
//            $.ajax({
//                type: 'get',
//                url: href,
//                dataType: 'json',
//                async: false,
//                success:function(response)
//                {
//                    cachedContent = response;
//                    $.jCache.setItem(keyCache, response);
//                }
//            });
//        }
//        
//        if (!cachedContent.error)
//        {
//
//            $this.parent().parent().find('li').removeClass('active');
//            $this.parent().addClass('active');
//            var $idbxh = $("#id_bxh_content_tab");
//            $idbxh.fadeOut(100, function(){
//                $idbxh.html(cachedContent.body).fadeIn(400);
//            });
//        }
//    });
//});


$('#search_bar .btn_search').hover(function(){	
    $(this).addClass('over');
}
,function(){
    $(this).removeClass('over');
}
)
$('#menu_portal .menu_product li').hover(function(){	
    $(this).addClass('over');
}
,function(){
    $(this).removeClass('over');
}
)
var flag = true;
function changePhoto(obj){
    $('.block_chandung .item').removeClass('active');
    /*$("#descPhoto").slideUp(100);*/
    /*setTimeout('$("#imgLink").fadeOut(300)',4700);*/
    var objTemp = $('.block_chandung .item:eq('+obj+')');
    var imgLink = '<a href="'+objTemp.attr('rev')+'"><img width="280" height="210" src="'+objTemp.attr('rel')+'" /></a>';
    var descLink = '<span><a href="'+objTemp.attr('rev')+'">'+objTemp.attr('title')+'</a></span>';
    objTemp.addClass('active');
    $('#imgLink').html(imgLink).fadeIn(300);
    $('#descPhoto').html(descLink).slideDown(300);
    if (objTemp.attr('desc')) $('#txtPhoto').html(objTemp.attr('desc'));
    if(flag==true)
    {
        v = setTimeout('$("#imgLink").fadeOut(300);$("#descPhoto").slideUp(300)',4600);
        if(obj+1 < $('.block_chandung .item').length){
				
            t = setTimeout("changePhoto("+(obj+1)+")",5000);
        }
        else{
            t = setTimeout("changePhoto(0)",5000);
        }
    }
    else
    {
        clearTimeout(v);
    }
}	
$(function(){
    $('.block_chandung .item').click(function(){
        clearTimeout(t);
        var index = $('.block_chandung .item').index(this);
        flag = false;
        changePhoto(index);
    });
    changePhoto(0);
});
$(function(){
    var numImagesLoad=1;
    var speedScroll=400;
    var numPageIndex=5;
    var numItems=$('.trandau_items li').length;
    var numImgBlockWidth=$('.trandau_items li').width();
    var numImgBlockHeight=$('.trandau_items li').height();
    for(var i=0;i<numItems;i++) $('.trandau_items li:eq('+i+')').css("left",(i*numImgBlockWidth));
    $('#prev_btn').hide();
    //$('.content_imglist').width(900);
    $('.trandau_items ul').height(numImgBlockHeight);
    $(".trandau_items #next_btn").click(function(){
        if(numPageIndex<(numItems/numImagesLoad)){
            $('#prev_btn').fadeIn();
            $(".trandau_items li").animate({
                left:'-='+numImgBlockWidth*numImagesLoad+'px'
            }, speedScroll);
            numPageIndex++;
            if(numPageIndex>=(numItems/numImagesLoad)){
                $('#next_btn').fadeOut();
            }
        }
    });
    $(".trandau_items #prev_btn").click(function(){
												
        if(numPageIndex>5){
            $('#next_btn').fadeIn();
            $(".trandau_items li").animate({
                left:'+='+numImgBlockWidth*numImagesLoad+'px'
            }, speedScroll);
            numPageIndex--;
            if(numPageIndex<=5){
                $('#prev_btn').fadeOut();
            }
								
        }
    });

})

$(function(){    
    $('#back-top').click(function(){
        $('html,body').animate({
            scrollTop: $("#container").offset().top - 25
        },'slow');
    });

    $('#print_content').click(function(){        
        var html = "<html>";
        html += "<head>";
        html += "<style type='text/css'></style>";
        html += "<link href='"+ css_url +"/reset.css' media='screen' rel='stylesheet' type='text/css' />";
        html += "<link href='"+ css_url +"/general.css' media='screen' rel='stylesheet' type='text/css' />";
        html += "<link href='"+ css_url +"/portal_thethao.css' media='screen' rel='stylesheet' type='text/css' />";
        html += "</head>";
        html += "<body>";
        html += "<div class='title_popup_print'>";
        html += "<div class='left'>";
        html += "<img src='"+img_url+"/graphics/img_vnexpress.jpg' alt='' class='logo_vne' />";
        html += "</div>";
        html += "<div class='right'>";
        html += "<a href='javascript:;' onclick = 'return window.print();' class='txt_print' title='In trang'><img src='"+img_url+"/buttons/btn_print.gif' alt='In trang' class='button_print' /></a>";
        html += "</div>";
        html += "<div class='clear'>&nbsp;</div>";
        html += "</div>";
        html += "<div class='block_print'><div id='box_details_news'>";
        html += "<div class='title_news'>" + $('div.main_content_detail .content_news .title_news').html() + "</div>";
        html += "<div class='short_intro'>" + $('div.main_content_detail .content_news .short_intro').html() + "</div>";
        html += "<div class='fck_detail'>" + $('div.main_content_detail .content_news .fck_detail').html() + "</div>";
        
        html += "</div>";
        html += "<div class='clear'>&nbsp;</div>"
        html += "</div>";
        html += "</body>";
        html += "</html>";

        var printWin = window.open('','print','left=300,top=0,width=550,height=600,toolbar=0,scrollbars=1,status  =0');
        printWin.document.write(html);        
        printWin.document.close();
        printWin.focus();
    });
});


/* share google+*/
(function() {
    var po = document.createElement('script');
    po.type = 'text/javascript';
    po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(po, s);
})();


$(function(){
    (function(){
        /** Toggle link external**/
        var container  = $('#top_page').find('.tk ul');
        $('#top_page').on('click', '#hasmore', function(){

            $(this).toggleClass('over').next().slideToggle(300).parent().toggleClass('start');

        });

    })();
});

/*
 * Init topbar, notifycation, menu user 
 */
$(function(){
    
    var dm = ('undefined' == typeof(domain))? 'http://'+document.domain:domain ;

    $.ajax({
        type:'GET',
        url: dm + '/index/statuslogin',
        dataType:'json',
        success: function(response){

            var currentUrl = window.location.href, 
            $data = $(response.topbar),
            $btLogin = $data.find('#btnLogin'),
            $btLogout = $data.find('#btnLogout'),
            $btRegister = $data.find('#btnRegister');

            if($btLogin.length){
                var loginUrl = $btLogin.attr('href');
                loginUrl += '?referersp='+ encodeURIComponent(currentUrl);
                $btLogin.attr('href', loginUrl);
            }

            if($btLogout.length){
                var logoutUrl = $btLogout.attr('href');
                logoutUrl += '?referersp='+ encodeURIComponent(currentUrl);
                $btLogout.attr('href', logoutUrl);
            }

            if($btRegister.length){
                var regUrl = $btRegister.attr('href');
                regUrl += '?referersp='+ encodeURIComponent(currentUrl);
                $btRegister.attr('href', regUrl);
            }
                        
            //append html
            $data.insertAfter($('#top_page').find('ul.left'));

            //load notifycation if logged
            if(response.userid){
                if(typeof(stNotifyDomain) != 'undefined'){
                    
                    window.bbNotifyInboxUrl = dm + '/' + response.userid + '/request';
                    
                    var script = document.createElement("script");
                    script.type = "text/javascript";
                    script.async = true;
                    script.src = stNotifyDomain + "/js/bb.notify.load.js";
                    document.documentElement.childNodes[0].appendChild(script);
                    script.onload = script.onreadystatechange = function(e){};
                }
                
                if(response.comment!=""){
                    $('#comment_post_button').parent().prepend(response.comment);
                }
                
                var container  = $('#top_page');
                container.on('click', '.exit-blog', function(){
                    container.find('.tk ul').slideToggle(200, function(){
                        var $this = $(this);
                        $this.is(':hidden') ? $this.prev().removeClass('active') : $this.prev().addClass('active')
                    });
                });

                container.find('.tk ul').clickoutside(function(e){
                    (e.target != $('#top_page').find('a.exit-blog')[0]) &&  container.find('.tk ul').hide().prev().removeClass('active');
                });
            }
        }
    });
});
